using UnityEngine;

namespace AudioStudio
{
    /// <summary>
    /// ������Ч��ʵʱ����3Dλ������
    /// </summary>
    [AddComponentMenu("AudioStudio/AnimationSound")]
    [DisallowMultipleComponent]
    public class AnimationSound : AudioGameObject
    {
        public Bank[] Banks;
        public bool PlayerControl = false;      ///决定event后面是否追加_PC后缀

        new void Awake()
        {
            LoadBanks();
            base.Awake();
        }

        new void OnDestroy()
        {
            base.OnDestroy();
            UnloadBanks();
        }

        //for animator
        public void PlaySound(string eventName)
        {
            if(AkSoundEngine.IsInitialized())
            {
                if(PlayerControl)
                {
                    string withSuffix = AudioManager.Instance.GetEventNameWithSuffix(eventName);
                    AudioManager.Instance.PlaySound(withSuffix, this.Player);
                }
                else
                {
                    AudioManager.Instance.PlaySound(eventName, this.Player);
                }
            }
        }

        public void StopSound(string eventName)
        {
            if(AkSoundEngine.IsInitialized())
            {
                if (PlayerControl)
                {
                    string withSuffix = AudioManager.Instance.GetEventNameWithSuffix(eventName);
                    AudioManager.Instance.StopSound(withSuffix, this.Player);
                }
                else
                {
                    AudioManager.Instance.StopSound(eventName, this.Player);
                }
            }
        }

        //for timeline playableasset
        public void Play(AudioEventPC events)
        {
            if(AkSoundEngine.IsInitialized())
            {
                events.Post(this.Player,this.PlayerControl);
            }
        }

        public void Stop(AudioEventPC events)
        {
            if(AkSoundEngine.IsInitialized())
            {
                events.Stop(this.Player,0,AkCurveInterpolation.AkCurveInterpolation_Linear,this.PlayerControl);
            }
        }

        void LoadBanks()
        {
            if (Banks != null)
            {
                for (var i = 0; i < Banks.Length; i++)
                {
                    Banks[i].Load();
                }
            }
        }


        void UnloadBanks()
        {
            if (Banks != null)
            {
                for (var i = 0; i < Banks.Length; i++)
                {
                    Banks[i].Unload();
                }
            }
        }
    }
}